package com.lovo.roomProject.classManager.dao.impl;

import com.lovo.roomProject.classManager.dao.ClaszDAO;
import com.lovo.roomProject.dao.BaseDAO;
import com.lovo.roomProject.pojo.ClaszDO;
import com.lovo.roomProject.pojo.CutPageBean;
import com.lovo.roomProject.pojo.StudentDO;

import java.sql.SQLException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

public class ClaszDAOImpl extends BaseDAO implements ClaszDAO {

    @Override
    public List listClasz() {
        this.getConnection();
        String sql = "SELECT * FROM t_class;";
        List list = new ArrayList();
        try {
            statement = con.prepareStatement(sql);
            rs = statement.executeQuery();
            while (rs.next()) {
                ClaszDO claszDO = new ClaszDO();
                claszDO.setId(rs.getInt("c_id"));
                claszDO.setName(rs.getString("c_name"));
                claszDO.setTeacher(rs.getString("c_teacher"));
                claszDO.setCreatDate(LocalDate.parse(rs.getString("c_create_date")));
                list.add(claszDO);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            close(con, statement);
        }
        return list;
    }

    @Override
    public void insert(ClaszDO claszDO) {
        this.getConnection();
        String sql = "INSERT INTO t_class(c_name,c_teacher,c_create_date) values (?,?,?)";
        try {
            statement = con.prepareStatement(sql);
            statement.setObject(1, claszDO.getName());
            statement.setObject(2, claszDO.getTeacher());
            statement.setObject(3, claszDO.getCreatDate());
            statement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con, statement);
        }
    }

    @Override
    public void delete(int id) {
        this.getConnection();
        String sql = "DELETE FROM t_class WHERE c_id = ?";
        try {
            statement = con.prepareStatement(sql);
            statement.setObject(1, id);
            statement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con, statement);
        }
    }

    @Override
    public CutPageBean listClaszByPage(int current) {
        this.getConnection();
        String selectSql = " select * ";
        String countSql = " select count(*) cnt ";
        String fromSql = " from t_class ";
        String limitSql = " limit ?,?";
        CutPageBean cutPageBean = new CutPageBean();
        List list = new ArrayList();
        try {
            statement = con.prepareStatement(selectSql + fromSql + limitSql);
            statement.setObject(1, (current - 1) * CutPageBean.PAGESIZE);
            statement.setObject(2, CutPageBean.PAGESIZE);
            rs = statement.executeQuery();
            while (rs.next()) {
                ClaszDO claszDO = new ClaszDO();
                claszDO.setId(rs.getInt("c_id"));
                claszDO.setName(rs.getString("c_name"));
                claszDO.setTeacher(rs.getString("c_teacher"));
                claszDO.setCreatDate(LocalDate.parse(rs.getString("c_create_date")));
                list.add(claszDO);
            }
            cutPageBean.setList(list);
            //计算总数的sql
            statement = con.prepareStatement(countSql + fromSql);
            rs = statement.executeQuery();
            rs.next();
            cutPageBean.setCount(rs.getInt("cnt"));
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            close(con, statement, rs);
        }
        return cutPageBean;
    }
}
